<%@ page language="java" contentType="text/html; charset=utf-8"
 pageEncoding="utf-8"%>
<%@ page import="java.io.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.net.*"%>
<%@page import="java.util.*"%>
<%@page import="com.augurit.gzcgwsp.webservice.SFTPUtil"%>
<%@page import="com.augurit.gzcgwsp.ywcmn.service.impl.YwDzgzService"%>
<%@page import="com.augurit.gzcgwsp.ywcmn.entity.YwDzgzList"%>
<%@page import="com.augurit.common.system.web.auth.form.LoginUserForm"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/xml; charset=utf-8">
</head>
<body>
<%
	response.setContentType("text/plain");
	PrintWriter pw = response.getWriter(); 
	String context =request.getSession().getServletContext().getRealPath("");
	String ajid=request.getParameter("ajid");
	String templateCode=request.getParameter("templateCode");
	LoginUserForm form=(LoginUserForm)request.getSession().getAttribute("SES_LOGIN_USER"); 
	String gzfilename=URLDecoder.decode(request.getParameter("fileTitle"),"utf-8");
	String formatDate = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
	int random = new Random().nextInt(1000000);
	
	String filename=formatDate+random+".ceb";
	String filepath="/resources/AttacheFile/";
	String loadPath = context+filepath;    //将要上传到的路径。
	String directory=context+File.separator+"resources"+File.separator+"AttacheFile";
	String uploadPath=directory+File.separator+filename;

	InputStream in = request.getInputStream();
	BufferedInputStream bis = new BufferedInputStream(in);
	File file = new File(loadPath);
	if(!file.exists()){
		file.mkdirs();
	}
	
	File  newfile= new File(loadPath + filename);
	if(newfile.exists()){
		newfile.createNewFile();
	}
	BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newfile));
	int b = 0;
	while((b = bis.read()) != -1){
		bos.write(b);
		bos.flush();
	}	
	bis.close();
	bos.close();
	in.close();
	SFTPUtil.uploadOtherFile(filepath+filename, uploadPath);
	YwDzgzService service=new YwDzgzService();
	YwDzgzList dzgz=new YwDzgzList();
	dzgz.setAjid(Long.valueOf(ajid));
	dzgz.setFilename(gzfilename);
	dzgz.setFilepath(filepath+filename);
	dzgz.setTemplateCode(templateCode);
	dzgz.setUploaddate(new Date());
	dzgz.setUsername(form.getUser().getUserName());
	String result=service.save(dzgz);
	pw.print(result);
%>
</body>
</html>

